package leetcode.code0466;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution1 implements IDebug {

	public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
		int len1 = s1.length();
		int len2 = s2.length();
		char[] c1 = s1.toCharArray();
		char[] c2 = s2.toCharArray();
		boolean[] dic1 = new boolean[26];
		ArrayList<Integer> list[] = new ArrayList[26];
		for (int i = 0; i < 26; i++) {
			list[i] = new ArrayList<>();
		}
		for (int i = 0; i < len1; i++) {
			char c = c1[i];
			int index = c - 'a';
			dic1[index] = true;
			list[index].add(i);
		}
		for (char c : c2) {
			if (!dic1[c - 'a']) {
				return 0;
			}
		}
		int p1 = 0;
		int p2 = 0;
		int loop = 1;
		while (p2 < len2) {
			char c = c2[p2];
			int index = c - 'a';
			List<Integer> l = list[index];
			if (p1 == len1) {
				p1 = 0;
				loop += 1;
			}
			p1 = this.find(l, p1);
			if (p1 == -1) {
				p1 = 0;
				loop += 1;
				p1 = this.find(l, p1);
			}
			p2++;
		}
		if (loop == 1) {
			// this.f(p1,p2,list);
		}
		return n1 / (n2 * loop);
	}

	private int find(List<Integer> list, int p1) {
		for (int i : list) {
			if (i >= p1) {
				p1 = i + 1;
				return p1;
			}
		}
		return -1;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(2, this.getMaxRepetitions("acb", 4, "ab", 2));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(1, this.getMaxRepetitions("abc", 1, "abc", 1));
	}

	@Override
	public void debug3() {
		HelpDebug.compare(1, this.getMaxRepetitions("a", 4, "aaa", 1));
	}

	@Override
	public void debug4() {
		HelpDebug.compare(5, this.getMaxRepetitions("bkacggdiuop", 21, "cbbagoo", 1));
	}

	public void debug5() {
		HelpDebug.compare(4, this.getMaxRepetitions("aaa", 3, "aa", 1));
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
